package com.zfj.LinkedList;

import com.zfj.Collection.Student;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

/**
 * @author zfj
 * @date 2022/3/5 -18:44
 * LinkedList使用
 * 存储结构：双向链表
 */
public class Demo1 {
	public static void main(String[] args) {
		LinkedList linkedList=new LinkedList();
		//添加元素
		//添加学生对象
		Student s1=new Student("张三",20);
		Student s2=new Student("李四",22);
		Student s3=new Student("王五",25);
		linkedList.add(s1);
		linkedList.add(s2);
		linkedList.add(s3);
		System.out.println(linkedList.toString());
		//删除
		linkedList.remove(s2);
		System.out.println("删除后："+linkedList.toString());
		//遍历
		for (int i = 0; i < linkedList.size(); i++) {
			System.out.println(linkedList.get(i));
		}
		//增强for
		System.out.println("======增强for=======");
		for(Object o:linkedList){
			Student s=(Student)o;
			System.out.println(s.toString());
		}
		//迭代器
		System.out.println("======迭代器=======");
		Iterator iterator = linkedList.iterator();
		while (iterator.hasNext()) {
			Student next = (Student) iterator.next();
			System.out.println(next);
		}
		//列表迭代器
		System.out.println("======列表迭代器======");
		ListIterator listIterator = linkedList.listIterator();
		while (listIterator.hasNext()) {
			Student next = (Student)listIterator.next();
			System.out.println(next);
		}
		//判断
		System.out.println(linkedList.contains(s1));
		System.out.println(linkedList.isEmpty());
		//获取
		System.out.println(linkedList.indexOf(s2));
	}
}
